# 通过正则表达式匹配，找出文件中所有访问记录里。
# 访问次数最多的前10个IP，访问最频繁的前10个页面

def analysisLog(path,regex):
    returnIPDict = {} # 记录返回的ip
    returnUrlDict = {} # 记录返回的url
    with open(path) as f:
        for line in f:
            res = regex.match(line)
            if res:
                dic = res.groupdict()
                ipCount = returnIPDict.get(dic['ip'],1)
                if ipCount:
                    ipCount += 1
                returnIPDict[dic['ip']] = ipCount

                urlCount = returnUrlDict.get(dic['url'],1)
                if urlCount:
                    urlCount += 1
                returnUrlDict[dic['url']] = urlCount
            else:
                # 记录不符合格式的数据
                with open('error.log','a+') as w:
                    w.write(line)
    return {'returnUrlDict':returnUrlDict,'returnIPDict':returnIPDict}
import re

regex = re.compile('(?P<ip>[\d{1,3}.]+) - - \[(?P<date>\d{2}/[A-Za-z]+/\d{4}:\d{2}:\d{2}:\d{2} \+\d{4})\] \
"(?P<method>.+) (?P<url>.+) (?P<http>.+)" (?P<status>\d{3}) \
(?P<size>\d+) "[^"]+" "(?P<browser>.+)" "[^"]"')

res = analysisLog('access.log-20191119',regex)

print('访问最多的页面为：',(sorted(res['returnUrlDict'].items(),key=lambda item:item[1],reverse=True)[:10]))
print('访问做多的ip为：',sorted(res['returnIPDict'].items(),key=lambda item:item[1],reverse=True)[:10])




